﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Catsland.Core
{
	public class CatsModel
	{
		public String _name { get; set; }
		public Material _material { get; set; }
		public Animation _animation { get; set; }

		public CatsModel(String name, Material material, Animation animation = null)
		{
			_name = name;
			_material = material;
			_animation = animation;
			if (_animation == null)
			{
				_animation = new Animation();
			}
		}

		public bool SaveToNode(XmlNode node, XmlDocument doc)
		{
			XmlElement model = doc.CreateElement("Model");
			node.AppendChild(model);

			model.SetAttribute("name", _name);
			model.SetAttribute("material", _material._name);

			_animation.SaveToNode(model, doc);

			return true;
		}

		public static CatsModel LoadFromNode(XmlNode node, Scene scene)
		{
			XmlElement model = (XmlElement)node;
			String name = model.GetAttribute("name");
			String materialName = model.GetAttribute("material");
			
			// animation
			XmlNode animation = model.SelectSingleNode("Animation");
			Animation newAnimation = Animation.LoadFromNode(animation, scene);

			CatsModel newModel = new CatsModel(name, scene._materialList.GetMaterial(materialName), newAnimation);

			return newModel;
		}
	}
}
